package com.Impl;

import com.dao.collect_productDao;
import com.entity.collect_product;
import com.service.collect_productService;
import com.util.DataBaseUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class collect_productServiceImpl implements collect_productService {
  @Override
  public List<collect_product> findALl() {
    List<collect_product> list = null;
    Connection con = null;
    try {
      con = DataBaseUtil.getCon();
      collect_productDao collect_productDao = new collect_productDaoImpl(con);
      list = collect_productDao.findALl();
    } finally {
      DataBaseUtil.closeAll(con,null,null);
    }
    return list;
  }

  @Override
  public collect_product findByIdOne(int cid) {
    collect_product collect_product = null;
    Connection con = null;
    try {
      con = DataBaseUtil.getCon();
      collect_productDao collect_productDao = new collect_productDaoImpl(con);
      collect_product = collect_productDao.findByIdOne(cid);
    } finally {
      DataBaseUtil.closeAll(con,null,null);
    }
    return collect_product;
  }

  @Override
  public boolean addCollect_Product(collect_product collect_product) {
    boolean flag = false;
    Connection con = null;
    try {
      con = DataBaseUtil.getCon();
      con.setAutoCommit(false);
      collect_productDao collect_productDao = new collect_productDaoImpl(con);
      int result = collect_productDao.checkName(collect_product.getCname());//collect_product.getCname()
      flag = collect_productDao.addCollect_Product(collect_product);

      System.out.println("check:"+result);
      if(result>0){
        //数据库有重复值 不允许添加
        flag = false;
        con.rollback();
      }else{
        con.commit();
      }
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    } finally {
      DataBaseUtil.closeAll(con,null,null);
    }
    return flag;
  }

  @Override
  public int checkName(String cname) {
    int result  = 0;
    Connection con = null;
    try {
      con = DataBaseUtil.getCon();
      collect_productDao collect_productDao = new collect_productDaoImpl(con);
      result = collect_productDao.checkName(cname);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DataBaseUtil.closeAll(con,null,null);
    }
    return result;
  }

  @Override
  public List<collect_product> findByBidAll(int bid) {
    List<collect_product> list = null;
    Connection con = null;
    try {
      con = DataBaseUtil.getCon();
      collect_productDao collect_productDao = new collect_productDaoImpl(con);
      list = collect_productDao.findByBidAll(bid);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DataBaseUtil.closeAll(con,null,null);
    }
    return list;
  }
}
